package com.bds.wm.action;

import com.alibaba.fastjson.JSONObject;
import com.bds.wm.model.common.UserSession;
import com.bds.wm.model.view.BrandName;
import com.bds.wm.model.view.KeyBrand;
import com.bds.wm.model.view.YearMonth;
import com.bds.wm.service.CityViewService;
import com.bds.wm.service.UserService;
import com.bds.wm.service.ViewService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
public class IndexController extends BaseAction {
    // 默认每10mb生成一个log文件
    Logger log = LoggerFactory.getLogger(getClass());
    // @Value("${application.hello:Hello Angel}")

    @Autowired
    private UserService userService;
    @Autowired
    private ViewService viewService;
    @Autowired
    private CityViewService cityViewService;


    @RequestMapping("/")
    public String index(Map<String, Object> map, HttpServletRequest request, Model model) {
        try {
            UserSession userSession = (UserSession) (request.getSession().getAttribute("sessionContext"));
            model.addAttribute("user_name", userSession.getUser_name());
        } catch (Exception e) {
            model.addAttribute("user_name", "Administrator");
        }
        return "ftl/index";
    }

    @RequestMapping("/city")
    public String city(Map<String, Object> map, HttpServletRequest request, Model model) {
        List<BrandName> brandNames = viewService.getBrandName();
        //List<SubCategoryName> subCategoryNames = viewService.getSubCategoryName();
        List<String> catNames = viewService.getCatNames();
        YearMonth latestMonth = viewService.getLatestMonth();
        List<KeyBrand> keyBrands = cityViewService.getKeyBrands();
        String yearMonth = String.valueOf(latestMonth.getYearMonth());
        if (yearMonth.toCharArray().length >= 6) {//201801
            String yearMonthF = yearMonth.substring(0, 4) + "-" + yearMonth.substring(4, 6);
            model.addAttribute("yearMonth", yearMonthF);
        }
        model.addAttribute("brandNames", JSONObject.toJSONString(brandNames));
        model.addAttribute("subCategoryNames", catNames);
        model.addAttribute("keyBrands", JSONObject.toJSONString(keyBrands));
        try {
            UserSession userSession = (UserSession) (request.getSession().getAttribute("sessionContext"));
            model.addAttribute("user_name", userSession.getUser_name());
        } catch (Exception e) {
            model.addAttribute("user_name", "Administrator");
        }
        return "ftl/city/wm_city";
    }

    @RequestMapping("/brand")
    public String brand(Map<String, Object> map, HttpServletRequest request, Model model) {
        try {
            UserSession userSession = (UserSession) (request.getSession().getAttribute("sessionContext"));
            model.addAttribute("user_name", userSession.getUser_name());
        } catch (Exception e) {
            model.addAttribute("user_name", "Administrator");
        }
        return "ftl/brand/wm_brand";
    }

    @RequestMapping("/competitor")
    public String competitor(Map<String, Object> map, HttpServletRequest request, Model model) {
        try {
            UserSession userSession = (UserSession) (request.getSession().getAttribute("sessionContext"));
            model.addAttribute("user_name", userSession.getUser_name());
        } catch (Exception e) {
            model.addAttribute("user_name", "Administrator");
        }
        return "ftl/competitor/wm_competitor";
    }

    @RequestMapping("/shop")
    public String shop(Map<String, Object> map, HttpServletRequest request, Model model) {
        try {
            UserSession userSession = (UserSession) (request.getSession().getAttribute("sessionContext"));
            model.addAttribute("user_name", userSession.getUser_name());
        } catch (Exception e) {
            model.addAttribute("user_name", "Administrator");
        }
        model.addAttribute("shopid","-1");
        return "ftl/shop/wm_shop";
    }
    @RequestMapping("/shop_with_shopid")
    public String shop_with_shopid(String shopid,HttpServletRequest request, Model model) {
        try {
            UserSession userSession = (UserSession) (request.getSession().getAttribute("sessionContext"));
            model.addAttribute("user_name", userSession.getUser_name());
        } catch (Exception e) {
            model.addAttribute("user_name", "Administrator");
        }
        model.addAttribute("shopid",shopid);
        return "ftl/shop/wm_shop";
    }
    @RequestMapping("/businessCircle")
    public String businessCircle(Map<String, Object> map, HttpServletRequest request, Model model){
        try {
            UserSession userSession = (UserSession) (request.getSession().getAttribute("sessionContext"));
            model.addAttribute("user_name", userSession.getUser_name());
        } catch (Exception e) {
            model.addAttribute("user_name", "Administrator");
        }
        return "ftl/businessCircle/wm_businessCircle";
    }



}
